package com.ssyt.service;

import java.util.List;
import com.ssyt.common.utils.JdbcUtil;
import com.ssyt.common.vo.Position;
import com.ssyt.common.vo.User;

public class UserService {

	/*
	 * 登陆验证
	 */
	public User loginInfo(User user) {
		String sql = "SELECT * FROM USER WHERE email=? and password=?";
		return JdbcUtil.getUniqueResult(User.class, sql, user.getEmail(),
				user.getPassword());
	}

	/*
	 * 执行账号设置
	 */
	public Integer updateUser(User user) {
		Integer resultNum = null;
		String sql = "UPDATE user SET username =?,phone=? WHERE id= ? ";
		resultNum = JdbcUtil.executeUpdate(sql, user.getUsername(),
				user.getPhone(), user.getId());
		return resultNum;

	}

	/*
	 * 修改密码
	 */
	public Integer updatePassWord(User user) {
		Integer resultNum = null;
		String sql = "UPDATE user SET password =? WHERE id= ? ";
		resultNum = JdbcUtil.executeUpdate(sql, user.getPassword(),
				user.getId());
		return resultNum;

	}

	/*
	 * 首页热门职位
	 */
	public List<Position> listLatestPostion() {
		StringBuilder sb = new StringBuilder();
		sb.append("	SELECT p.name , ct.name as city$name,p.publish_time,p.salary_begin,p.salary_end,"
				+ " p.work_experience,p.education,p.treatment,c.name AS company$name,t.name AS trade$name,"
				+ "c.nature,c.purpose ");
		sb.append("FROM position p,company c,trade t,cities ct ");
		sb.append("WHERE p.company_id=c.id  AND c.trade_id=t.id AND p.city_id=ct.id ");
		sb.append("ORDER BY p.id DESC ");
		sb.append("LIMIT 10");
		System.out.println(sb.toString());
		return JdbcUtil.executeQuery(Position.class, sb.toString());
	}

	/*
	 * 密码验证
	 */
	public User checkUser(User user) {

		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM USER WHERE 1=1 ");
		if (user.getId() != null) {
			sql.append(" AND id = " + user.getId() + " ");
		}
		/*
		 * if(user.getPassword()!=null){
		 * sql.append(" AND password = '"+user.getPassword()+"'" ); }
		 * if(user.getEmail() !=null){
		 * sql.append(" AND email = '"+user.getEmail()+"'" ); }
		 * if(user.getUsername() !=null){
		 * sql.append(" AND username = '"+user.getUsername()+"'" ); }
		 * if(user.getIs_active() !=null){
		 * sql.append(" AND is_active = "+user.getIs_active()+" " ); }
		 * if(user.getPhone() !=null){
		 * sql.append(" AND phone = '"+user.getPhone()+"'" ); }
		 * if(user.getRole_type() !=null){
		 * sql.append(" AND role_type = '"+user.getRole_type()+"'" ); }
		 */
		return JdbcUtil.getUniqueResult(User.class, sql.toString());

	}
	/*
	 * 验证邮箱
	 */
	public User checkMail(User user){
		String sql="SELECT * FROM USER WHERE EMAIL= '"+user.getEmail()+"'";
		user=JdbcUtil.getUniqueResult(User.class, sql);
		return user;
	}

	public int addUser(User user) {
		
		String sql="INSERT  INTO USER"
				+ "(email,password,is_active,role_type)"
				+ " VALUES"
				+ "('"+user.getEmail()+"','"
				+user.getPassword()+"',"
				+user.getIs_active()+","
				+user.getRole_type()+")";
		System.out.println(sql);
		 return JdbcUtil.executeUpdate(sql);
		
	}
}
